package com.wpre.blogsystem.common.advice;

import com.wpre.blogsystem.common.pojo.Result;
import com.wpre.blogsystem.exception.BlogException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-04
 * Time: 14:08
 */
@ResponseBody
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Result handler(Exception e) {
        log.error("发生异常,e{ }", e);
        //log.error("发生异常,e{ }", e);
        return Result.fail("发生异常");
    }

    @ExceptionHandler(BlogException.class)
    public Result blogExceptionHandler(BlogException e) {
        log.error("发生错误,e{ }", e);
        return Result.fail("发生错误");
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)//设置状态码
    @ExceptionHandler(NoResourceFoundException.class)
    public Result NoResourceFoundExceptionHandler(NoResourceFoundException e) {
        log.error("文件资源不存在,e{ }", e);
        return Result.fail("文件资源不存在");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HandlerMethodValidationException.class)
    public Result HandlerMethodValidationExceptionHandler(HandlerMethodValidationException e) {
        log.error("参数不合法,e{ }", e);
        return Result.fail("参数不合法");
    }
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result HandlerMethodValidationExceptionHandler(MethodArgumentNotValidException e) {
        log.error("参数不合法,e{ }", e);
        return Result.fail("参数不合法");
    }

}
